<template>
	<view class="content">
		<!-- 搜索 -->
		<u-gap></u-gap>
		<view v-if="is_admin">
			<view class="search-box">
				<u-input placeholder="搜索项目名称" v-model="keyword" border style="flex: 1;margin-right: 10px" />
				<u-button type="primary" size="medium" @click="jumpEdit('')">新增</u-button>
			</view>

			<view>
				<u-gap />
				<u-loading :show="loading" mode="flower">加载中</u-loading>
				<view class="item-box"  v-for="item in filteredRecords">
					<view>
						<view>项目名称:{{item.name}}</view>
						<view>经办人:{{item.jinbanren}}</view>
					</view>
					<view class="footer">
						<u-button type="primary" @click="jumpEdit(item._id)" size="medium">修改</u-button><br/>
						<u-button type="success" size="medium" @click="jumpQrcode(item._id)">查看二维码</u-button>
					</view>
				</view>
			</view>
		</view>
		<view v-else style="font-size: 22px;padding-top: 50px;text-align: center">仅管理员可见</view>


		<u-modal v-model="showAdminModal" show-cancel-button @cancel="this.showAdminModal=false" @confirm="adminConfirm" title="如果您是管理员请输入验证码">
			<view class="slot-content" style="padding: 10px 20px;">
				<u-input v-model="admin_code"/>
			</view>
		</u-modal>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword:'',
				records: [],
				loading:false,
				showAdminModal:false,
				admin_code:'',
				is_admin:0,
			}
		},
		onLoad() {

		},
		onShow(){
			//判断是否是管理员
			let is_admin = uni.getStorageSync("is_admin")
			if (!is_admin) {
				this.showAdminModal = true
			} else {
				this.is_admin = 1
			}
			this.fetchRecord()
		},
		computed:{
			filteredRecords(){
				if (!this.keyword) {
					return this.records
				}
				return this.records.filter(v=>{
					return v.name.indexOf(this.keyword) > -1
				})
			}
		},
		methods: {
			jumpEdit(id) {
				uni.navigateTo({url:'/pages/edit/edit?id='+id})
			},
			jumpQrcode(id) {
				uni.navigateTo({url:'/pages/qrcode/qrcode?id='+id})
			},
			fetchRecord() {
				if (this.loading) {
					return
				}
				this.loading = true
				uni.showLoading({title:'正在获取数据...'})
				uniCloud.callFunction({
					name:'record_list',
					data:{}
				}).then(res=>{
					this.records = res.result
					this.loading = false
					uni.hideLoading()
				}).catch(e=>{
					uni.showModal({title:'错误提示', content:e})
					this.loading = false
					uni.hideLoading()
				})
			},
			adminConfirm() {

				if (this.admin_code == '282931') {
					uni.setStorageSync("is_admin", 1)
					this.is_admin = 1
				} else {
					uni.showModal({title:'提示', content:'验证码不正确，无权查看'})
				}
			}
		}
	}
</script>

<style>
	.search-box{
		display: flex;
		flex-direction: row;
		padding: 0px 10px;
	}
	.item-box {
		padding: 0px 10px 10px 10px;
		border-bottom: 1px solid #4cd964;
	}
	.footer {
		margin-top: 5px;
		display: flex;
		justify-content: space-around;
		flex-direction: row;
	}
</style>
